package com.example.afinal.Audio;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.afinal.MainActivity;
import com.example.afinal.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddVoiceActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivQuan;
    private TextView tvLuyin;
    private TextView tvWancheng;
    AudioRecorder audioRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_voice);
        ivQuan = (ImageView) findViewById(R.id.iv_quan);
        tvLuyin = (TextView) findViewById(R.id.tv_luyin);
        tvWancheng = (TextView) findViewById(R.id.tv_wancheng);

        ivQuan.setOnClickListener(this);
        tvWancheng.setOnClickListener(this);
        audioRecorder = AudioRecorder.getInstance();

    }

    //申请录音权限

    private static final int GET_RECODE_AUDIO = 1;

    private static String[] PERMISSION_ALL = {
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    /**
     * 申请录音权限
     */
    public static void verifyPermissions(Activity activity) {
        boolean permission = (ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED)
                || (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
                || (ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED);
        if (permission) {
            ActivityCompat.requestPermissions(activity, PERMISSION_ALL,
                    GET_RECODE_AUDIO);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_quan:
                try {
                    if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_NO_READY) {
                        //初始化录音
                        String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
                        audioRecorder.createDefaultAudio(fileName);
                        audioRecorder.startRecord(null);
                        tvLuyin.setText("暂停");
                        ivQuan.setImageDrawable(getResources().getDrawable(R.drawable.hongquan));

                    } else {
                        //暂停录音
                        audioRecorder.pauseRecord();
                        tvLuyin.setText("录音");
                        ivQuan.setImageDrawable(getResources().getDrawable(R.drawable.lanquan));
                    }
                } catch (IllegalStateException e) {
                    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_wancheng:
                if (audioRecorder.getStatus() != AudioRecorder.Status.STATUS_NO_READY) {
                    audioRecorder.stopRecord();
                    Toast.makeText(this, "成功添加录音", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "没有添加录音", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("sta", 2);
                startActivity(intent);
                break;
        }
    }
}
